<?php

class Score_Model_Normal extends Score_Model_Score
{

    /**
     * @param int $sn 配課代號
     * @param int $stage 階段次
     */
    public function __construct($sn,$stage)
    {
        parent::__construct($sn);
        $this->setStage($stage);
    }

    /**
     *  設定階段
     * @param $stage
     */
    public function setStage($stage)
    {
        //   如果是全學期打一次成績
        if ($this->_subjectSet->scored_mode == 0 ) {
            $stage = Score_Model_Stage::WHOLE_STAGE_SCORE;
        }
        else {
            if ($stage ==  Score_Model_Stage::WHOLE_STAGE_SCORE)
            $stage = 0;
            if (!$stage ) {
                //  查詢目前應輸入的階段
                $q = Doctrine_Query::create()
                ->select('MAX(test_sort) AS mm')
                ->leftJoin('Student_Model_StudentScoreStage')
                ->where('subject_sn=?', $this->_teacherCourse->subject_sn)
                ->andWhere('class_sn=?',$this->_teacherCourse->class_sn)
                ->andWhere('sendmit=?', 'Y');
                $res = $q->fetchOne();

                $stage = $res->mm;
                if ($stage) {
                    $stage_sn = $res->sn;
                    // 如果已傳送 次數小於總考次時,自動切換到下一階段
                    if ($stage < $this->_examine->period)
                    $stage++;
                }
                else
                $stage =  1;
            }
        }
        $this->_currStage = $stage;
    }



    /**
     * 平時成績記錄
     * @return unknown_type
     */
    public function getScores($testOrder='')
    {
        if ($this->_isElective) {
            $q = Doctrine_Query::create()
            ->select('s.student_sn')
            ->from('Student_Model_StudentScoreElectiveStudent s')
            ->leftJoin('s.Student_Model_StudentScoreElectiveGroup g')
            ->where('g.teacher_course_sn=?',$this->_teacherCourse->sn );
            foreach($q->fetchArray() as $row)
            $studentArr[] = $row['student_sn'];
        }

        $q = Doctrine_Query::create()
        ->select('a.*, b.subject_sn as subject_sn, b.test_name AS test_name, b.test_sort AS test_sort, b.test_order AS test_order, b.weighted AS weighted')
        ->from('Student_Model_StudentScoreNormalDetail a')
        ->leftJoin('a.Student_Model_StudentScoreNormal b')
        ->where('b.subject_sn=?',$this->_teacherCourse->subject_sn)
        ->andWhere('b.test_sort=?',$this->_currStage);
        if ($this->_isElective)
        $q->whereIn('a.student_sn',$studentArr);
        else
        $q->andWhere('b.class_sn=?', $this->_teacherCourse->class_sn);
        //->andWhere('b.class_sn=?', $this->_teacherCourse->class_sn);
        if ($testOrder)
        $q->andWhere('b.test_order=?', $testOrder);
        else
        $q->orderBy('b.test_order');

        $res = $q->fetchArray();
        $tempArr = array();

        foreach ($res  as $row) {
            $tempArr[$row['test_order']]['test_name'] = $row['test_name'];
            $tempArr[$row['test_order']]['weighted'] = $row['weighted'];
            $tempArr[$row['test_order']]['score'][$row['student_sn']] = $row['score'];
        }
        return $tempArr;
    }


    /**
     * 各次平均
     * @return unknown_type
     */
    public function getScoreAverage()
    {
        if ($this->_isElective) {
            $q = Doctrine_Query::create()
            ->select('s.student_sn')
            ->from('Student_Model_StudentScoreElectiveStudent s')
            ->leftJoin('s.Student_Model_StudentScoreElectiveGroup g')
            ->where('g.teacher_course_sn=?',$this->_teacherCourse->sn );
            foreach($q->fetchArray() as $row)
            $studentArr[] = $row['student_sn'];
        }

        $q = DOctrine_Query::create()
        ->select('a.student_sn , b.class_sn AS class_sn, SUM(a.score * b.weighted)/SUM(b.weighted) AS avg')
        ->from ('Student_Model_StudentScoreNormalDetail a')
        ->leftJoin('a.Student_Model_StudentScoreNormal b')
        ->where('b.subject_sn=?',$this->_teacherCourse->subject_sn)
        ->andWhere('b.test_sort=?',$this->_currStage);
        //->andWhere('b.class_sn=?', $this->_teacherCourse->class_sn)
        if ($this->_isElective)
        $q->andWhereIn('a.student_sn',$studentArr);
        else
        $q->andWhere('b.class_sn=?', $this->_teacherCourse->class_sn);

        $q->groupBy('a.student_sn');

        $res = $q->fetchArray();
        $arr = array();
        foreach($res as $row) {
            if ($this->_isElective)
            $arr[$row['class_sn'].'-'.$row['student_sn']] = sprintf("%0.2f", $row['avg']);
            else
            $arr[$row['student_sn']] = sprintf("%0.2f", $row['avg']);
        }
        return $arr;
    }


    /**
     * 取下次平時成績次別
     * @return integer
     */
    public function getNextOrder()
    {
        $q = Doctrine_Query::create()
        ->select('MAX(test_order) AS mm')
        ->from('Student_Model_StudentScoreNormal')
        ->where('subject_sn=?',$this->_teacherCourse->subject_sn)
        ->andWhere('test_sort=?',$this->_currStage)
        ->andWhere('class_sn=?', $this->_teacherCourse->class_sn);
        $res = $q->fetchOne();
        if ($res)
        $mm = $res->mm +1;
        else
        $mm =1;
        return $mm;
    }

}